/* 缓动插件 */
// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	}
});

(function() {
	$.suning || ($.suning = {});
	$.suning.productView = {};
	
	//继承
	$.suning.productView.extend = function (subClass, superClass) {
		var F = function() {};
		F.prototype = superClass.prototype;
		subClass.prototype = new F();
		superClass.prototype.constructor = superClass;
		subClass.prototype.constructor = subClass;
		subClass.base = superClass.prototype;
	}
})();
//快捷方式
$PV = $.suning.productView;//快捷方式


//选择城市
$PV.cityBox = function() {
	var city = $("#citybox");
	var btn,isopen = false,ref, cityc,offset,poffset, active = false, timeout;
	var url = 'city.htm';
	var nowprovince = "";
	var onChange;
	return {
		init: function(fn) {
			onChange = fn || (function(cname) {alert(cname)})
			if (city.length == 0) {
				return;
			}
			ref = this;
			
			var iframe = document.createElement("iframe");
			iframe.id = "temp_iframe";
			btn = $("#citybox_btn").click(function(e) {
				city.after(iframe);
				if(city.css('display')=="none"){
					$(this).addClass('select');	
					city.show();
					$(iframe).css({
						width : city.width(),
						height : city.height(),
						position : "absolute",
						"z-index" : 10,
						opacity : 0,
						top : 25,
						left : 75
					});
				}else{
					$(this).removeClass('select');	
					city.hide();
				}											   
				e.preventDefault();
				
			})
			
			city.mouseover(function(){
				$(this).show();
				$("#citybox_btn").addClass('select');
				$("#citybox_btn02").addClass('select');
			}).mouseleave(function(){
				$(this).hide();
				$("#temp_iframe").remove();
				$("#citybox_btn").removeClass('select');
				$("#citybox_btn02").removeClass('select');
			});
			
			city.find("table a").click(function(e) {
				
				ref.x=parseInt($(this).offset().left-$(".province").offset().left);
				ref.y=parseInt($(this).offset().top-$(".province").offset().top);
				e.preventDefault();
				if (cityc != null) {
					cityc.remove();
					cityc = null;
				}
				active = false;
				clearTimeout(timeout);
				nowprovince = this.innerHTML;
				poffset = $(this).offset();
				var _this=this;
				(function() {
					var qp = nowprovince;
					$.get(url, function(data) {
						if (qp == nowprovince) {
							ref.addCity(data,$(_this).parent());
						}
				})})()
				city.find("table a").css({"z-index":0,"position":"relative"});
				$(this).css("z-index",100000);
				//$(this).css("border","1px solid #999999");
				$(this).css("position","relative");
				$(this).addClass('on').parent().parent().siblings().find('a').removeClass('on');
				$(this).addClass('on').parent().siblings().find('a').removeClass('on');
				$(this).addClass('on').siblings().removeClass('on');
				
			}).mouseleave(function(e) {
				timeout = setTimeout(function() {
					if (!active && cityc != null) {
						cityc.remove();
						$(".province").find('a').removeClass('on');
						cityc = null;
						city.css({"height":"auto"});
					}
				},200)
			});
			
			/* 关闭按钮 */
			city.find(".closeIco").click(function(event){
				city.hide();
				event.preventDefault();
			});
		},
		changeView: function() {
			city.css("display", isopen ? "none" : "block");
			if (!offset && !isopen) {
				offset = city.offset();
			}
			isopen = !isopen;
		},
		addCity: function(str,obj) {
			var re = /([A-Za-z]+)(.[^\|]+)/g;
				var o = {}
				while(r = re.exec(str)) {

					if (!o[r[1]]) {
						o[r[1]] = [r[2]];
					} else {
						o[r[1]].push(r[2]);
					}
				}

				var loc = 0;
				var template = '<table class="citys"><tr>';
				for (var i in o) {
					var temp = "";
					var temparr = o[i];
					for (var j=0 ;j<temparr.length; j++) {
						temp += '<a href="#">' + temparr[j] + '</a>'
					}
					template += '<td class="td03">' + temp + '</td>';
					template += "</tr><tr>";
					
					loc++;
				}
				template = template.substring(0, template.length-5) + "</table>";
				cityc = $(template);
				city.append(cityc);
				if(this.y+cityc.height()<180){
					city.css({"height":205});
				}else{
					city.css({"height":this.y+cityc.height()+30});
				}
				cityc.mouseover(function(e) {
					active = true;
				}).mouseleave(function(e) {
					active = false;
					cityc.remove();
					cityc = null;
					city.css({"height":"auto"});
					$(".province").find('a').removeClass('on');
				});
				cityc.find("a").click(function(e) {

					//onChange(this.innerHTML,this.name);
					$("#citybox_btn").removeClass('select');
					$("#citybox_btn02").removeClass('select');
					setTimeout(function(){
						city.hide();
						$("#citybox_btn").removeClass('select');
						$("#citybox_btn02").removeClass('select');	
						city.css({"height":"auto"});
						$("#temp_iframe").remove();
					},300);		
					$('#citybox_btn .ctext').html(this.innerHTML);
					$('#citybox_btn02').html(this.innerHTML);
					e.preventDefault();
				});
				cityc.css({left:10+"px" ,top:this.y+20+"px" });
				//cityc.css({left:this.x+"px" ,top:this.y+20+"px" });
		}
	}
}
/**
 * 	Add by 12050231
 */

$PV.Ready = {
	init : function(){
		

		//大图展示
		this.pro_view_slide();

		//放大镜
		this.pro_imgzoom(800, 800);

		//倒计时
		
		new this.pro_time(600000, this.pro_reTime, "#timeCheck", this.pro_time_callback).start();
		new this.pro_time(600000, this.pro_reTime, "#timeCheck1", this.pro_time_callback).start();
		new this.pro_time(600000, this.pro_reTime, "#timeCheck2", this.pro_time_callback).start();
		new this.pro_time(600000, this.pro_reTime, "#timeCheck3", this.pro_time_callback).start();


		
	},
	//
	pro_imgzoom : function(w, h) {
		var zoompos = {x:0,y:0};//定义一个对象，缓存x,y变量.
		var $pre = $("#preview");
		var $preimg = $pre.find("img");
		var p_w  = w;
		var p_h  = h;

		setTimeout(function(){
			if($("#PicView").find("img").height() < 400){

				$("#PicView").find("img").animate({"margin-top" : 30}, 1000);
				p_w = 612;
				p_h = 520;
				$("#preView_box").find("img").css({"margin-top" : 8})

			}
		},80);
		var delay;
		if(!!window.ActiveXObject&&!window.XMLHttpRequest){
			$pre.after("<iframe id='maskA'  style='position:absolute;border:none;opacity:0;filter:alpha(opacity=0);z-index:1;display:none;'></iframe>");
		}
		$("#PicView").bind("mouseover",function(e){
			var _e = e;
			var $this  = $(this);
			var img = new Image();
			delay = setTimeout(function(){
				$(img).load(function(){
				var $zoom = $this.find(".zoomplePopup");
				$zoom.show();
				$pre.fadeIn();
				
				$("#maskA").css({top:0,left:402,opacity:0,width:$pre.width(),height:$pre.height()}).show();
				$preimg.attr("src", $preimg.attr("src2"));
				PositionPopupZoom( $this , $zoom , _e.pageX , _e.pageY , p_w , p_h );
				$this.bind("mousemove",function(e){
					setTimeout(function(){
						PositionPopupZoom( $this , $zoom , e.pageX , e.pageY , p_w , p_h);
					},10);
				});
				}).attr("src", $preimg.attr("src2"));
			},300);
			
		}).bind("mouseleave",function(){
			if(!!window.ActiveXObject&&!window.XMLHttpRequest){
				$("#maskA").hide();
			}
			var $this  = $(this);
			var $zoom = $this.find(".zoomplePopup");
			$zoom.hide();
			$pre.fadeOut();
			$this.unbind("mousemove");
		}).mouseout(function(){
			clearTimeout(delay);
		});
		/*
			wrap : 当前绑定的元素
			zoom : 当前绑定的元素里的popupzoom元素
			x, y : 当前鼠标在页面上的位置
			w, h : 放大的图片的高度和宽度,用来计算比例
		*/
		function PositionPopupZoom( wrap , zoom , x , y , w , h){
			var wrapLeft = wrap.offset().left;
			var wrapTop =  wrap.offset().top;
			var zoomWidth = zoom.width();
			var zoomHeight = zoom.height();
			var wrapWidth = wrap.width();
			var wrapHeight = wrap.height();
			
			zoompos.x =  x -wrapLeft - (zoomWidth/2);
			zoompos.y =  y -wrapTop- (zoomHeight/2);
			if( zoompos.x <= 0 ){
				zoompos.x  =  0;
			}
			if( zoompos.y <= 0 ){
				zoompos.y  =  0;
			}
			if( zoompos.x + zoomWidth >= wrapWidth){
				zoompos.x  = wrapWidth  -  zoomWidth; 
			}
			if( zoompos.y + zoomHeight >= wrapHeight){
				zoompos.y  = wrapHeight  -  zoomHeight; 
			}
			//放大比例
			var xRatio = w / wrapWidth;  
			var yRatio = h / wrapHeight;
			//设置位置
			zoom.css({left:zoompos.x,top:zoompos.y});
			$preimg.css({left: -(zoompos.x * parseFloat(xRatio)) , top:  -(zoompos.y * parseInt(yRatio)) }); 
		}		

	},

	pro_view_slide : function(){
		var delay;
		var i = 0;
		var box = $("#preView_box");
		box.find("li").mouseover(function(){
			var _this = $(this);
			delay = setTimeout(function(){
				_this.addClass("cur").siblings().removeClass("cur");
				$("#PicView").find("img").attr("src",_this.find("img").attr("src2"));
				$("#preview").find("img").attr("src2", _this.find("img").attr("src2"));
			},200);
			
		}).mouseout(function(){
			clearTimeout(delay);
		});

		var up = $("#preView_box").find("p.up");
		var down = $("#preView_box").find("p.down");
		var len = $("#preView_box").find("li").length;
		var ul = $("#preView_box").find("ul");
		if(len <= 4){
			len = 4;
			down.find("a").addClass("false");
			up.find("a").addClass("false");
			return;
		}
		down.click(function(){
			i++;
			up.find("a").removeClass("false");
			if(i >= len - 4){
				i = len - 4;
				$(this).find("a").addClass("false");
			}
			move(i);
		})

		up.click(function(){
			down.find("a").removeClass("false");
			i--;
			if(i <= 0){
				i = 0;
				$(this).find("a").addClass("false");
			}
			move(i);
		})
		
		function move(i){
			ul.stop(true).animate({"margin-top" : -103 * i});
		}

	},
	
	/**
	 * [pro_time description]
	 * @param  {[Number]}   t      [时间，单位为毫秒]
	 * @param  {[function]}   func     [时间innerHTML]
	 * @param  {[type]}   obj      [显示时间元素]
	 * @param  {Function} callback [回调函数，可选]
	 * @return {[type]}            [description]
	 */
	pro_time : function(t,func,obj,callback){
		var self=this;
		this.time = t;
		this.start=function(){
			var tt=self.time/1000;
			var o={};
			o.time = self.time;
			o.seconds = parseInt(tt%60);
			o.minutes = parseInt((tt/60)%60);
			o.hours = parseInt((tt/60/60)%24);
			o.days = parseInt(tt/60/60/24);
			if(o.seconds < 10){o.seconds="0"+o.seconds;}
			if(o.minutes < 10){o.minutes="0"+o.minutes;}
			func(o,obj, callback);
			if(self.time >= 0){
				setTimeout(self.onRetime, 1000);
			}
		};
		this.onRetime = function(){
			self.time -= 1000;
			self.start();
		};	
	},
	pro_time_callback : function(){
		alert("抢购结束");
	},
	pro_reTime : function(time, obj, callback){
		if (time.time >= 0) {
			//$(obj).html('剩<em>' + time.days + '</em>天<em>' + time.hours + '</em>小时<em>' + time.minutes + '</em>分<em>' + time.seconds + '</em>秒');
			$(obj).find(".hour").html(time.hours);
			$(obj).find(".minute").html(time.minutes);
			$(obj).find(".second").html(time.seconds);

		}else{
			callback();
		}
	},
}

//执行开始
$(function(){
	
	$PV.cityBox().init();
	$PV.Ready.init();

})